package com.czxy.home.utils;

/**
 * @author xuhongzu
 * @version 1.0
 * @date 2019/4/24
 */


import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;

public class TestEmail {
    /**
     *
     * @return
     */
    public static String getEmailVc(){
        return new Random().nextInt(10000)+"";
    }
    public static String sendEmail(String email) throws Exception {
        String trueEmail = new Random().nextInt(10000)+"";
        // 0.1 服务器的设置
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.qq.com");
        props.setProperty("mail.smtp.auth", "true");
        // 0.2 账号和密码
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("1952683402@qq.com", "cdcnevzcsmtrbiff");
//                return new PasswordAuthentication("admin@xhz.com", "admin");
            }
        };
        // 1 与126服务器建立连接：Session
        Session session = Session.getDefaultInstance(props, authenticator);

        // 2 编写邮件：Message
        Message message = new MimeMessage(session);
        // 2.1 发件人（模拟账号）
//        message.setFrom(new InternetAddress("admin@xhz.com"));
        message.setFrom(new InternetAddress("1952683402@qq.com"));
        // 2.2 收件人 , to:收件人 ， cc ：抄送，bcc：暗送（密送）。（模拟账号）

//		message.setRecipient(RecipientType.TO, new InternetAddress("2029753430@qq.com"));//于复鑫
//		message.setRecipient(RecipientType.TO, new InternetAddress("3559881941@qq.com"));//罗荧
        message.setRecipient(RecipientType.TO, new InternetAddress(email));


        // 2.3 主题
        message.setSubject("宏羽博客短信验证码");
        // 2.4 内容
        message.setContent("尊敬的用户你好，欢迎您注册宏羽博客，您的验证码是："+trueEmail, "text/html;charset=UTF-8");

        // 3 将消息进行发送：Transport
        Transport.send(message);
        return trueEmail;
    }
}
